package com.dcy.znoa.controller.estate;

import com.aliyuncs.exceptions.ClientException;
import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.estate.model.service.AddEstateServiceModel;
import com.dcy.znoa.controller.estate.model.service.SelectEstateServiceModel;
import com.dcy.znoa.controller.estate.model.service.UpdateEstateServiceModel;
import com.dcy.znoa.domain.estate.EstateService;
import com.dcy.znoa.service.estate.EstateServiceService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import static com.dcy.znoa.common.util.pojo.JsonCommon.INSERT_ERROR_CODE;
import static com.dcy.znoa.common.util.pojo.JsonCommon.SUCCESS_CODE;
import static com.dcy.znoa.common.util.pojo.JsonCommon.UPDATE_ERROR_CODE;

/**
 * <p>
 * 物业服务表 前端控制器  服务单
 * </p>
 * @author cook
 * @since 2018-05-21
 */
@Controller
@RequestMapping("/estateService")
public class EstateServiceController extends BaseController {

    @Autowired
    EstateServiceService esService;
    //列表查询 分页  通用Controller
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectEstateServiceModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonForList("404", bindingResult.getFieldError().getDefaultMessage());
        }
        return esService.selectList(model);
    }

    //单条数据查询 通用Controller
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectById")
    public JsonCommon selectById(Long esserId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (esserId == null) {
            return new JsonCommon("404", "esserId不能为空");
        }
        return  new JsonCommon(esService.selectById(esserId));
    }

    //APP
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddEstateServiceModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        EstateService cp = new EstateService();
        BeanUtils.copyProperties(cp, model);
        cp.setCreateOperId(getCurrentUser().getOperId());
        cp.setCompId(getCurrentUser().getCompId());
        cp.setCreateTime(new Date());
        cp.setValidFlag(1);
        Integer result = esService.insert(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(INSERT_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateEstateServiceModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
;
        Integer result = esService.updateById(model);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(UPDATE_ERROR_CODE);
        }
    }



    public static void main(String args[]){
        System.out.print(System.currentTimeMillis());
    }



}
